package com.geen.tuoke.utils.ext
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.geen.tuoke.R
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog


//loading框
private var loadingDialogBuilder: QMUITipDialog? = null

/**
 * 打开等待框
 */
fun AppCompatActivity.showLoadingExt(message: String = getString(R.string.text_loading_message)) {
    if (!this.isFinishing) {

        loadingDialogBuilder?.let {
            dismissLoadingExt()
        }
        loadingDialogBuilder =
            QMUITipDialog.Builder(this).setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord(message).create()
        loadingDialogBuilder?.show()
    }
}


/**
 * 打开等待框
 */
fun Fragment.showLoadingExt(message: String = getString(R.string.text_loading_message)) {

    activity?.let {
        if (!it.isFinishing) {
            if (loadingDialogBuilder == null) {
                loadingDialogBuilder =
                    QMUITipDialog.Builder(it).setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                        .setTipWord(message).create()
            }
            loadingDialogBuilder?.show()
        }
    }
}

/**
 * 关闭等待框
 */
fun AppCompatActivity.dismissLoadingExt() {
    loadingDialogBuilder?.dismiss()
    loadingDialogBuilder = null
}

/**
 * 关闭等待框
 */
fun Fragment.dismissLoadingExt() {
    loadingDialogBuilder?.dismiss()
    loadingDialogBuilder = null
}
